package msg.chestionar.ejb.beans;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.Query;

import msg.chestionar.ejb.bi.IntrebareBI;

import com.msg.chestionar.entities.IntrebareE;
import com.msg.chestionar.to.IntrebareTO;

@Stateless
public class IntrebareBA implements IntrebareBI {
	
	
	private EntityManager em;

	@Override
	public IntrebareTO addIntrebare(IntrebareTO intrebareTO) {

		IntrebareE intrebareE = getIntrebareE(intrebareTO);

		this.em.persist(intrebareE);
		// this.em.refresh(intrebareE);

		return intrebareE.getIntrebareTO();
	}

	public IntrebareE getIntrebareE(IntrebareTO intrebareTO) {
		// TODO Auto-generated method stub
		return null;
	}

	public IntrebareTO modificaIntrebare(IntrebareTO intrebareTO) {
		IntrebareE intrebareE = this.em.find(IntrebareE.class, intrebareTO
				.getId());

		intrebareE.setText(intrebareTO.getText());
		intrebareE.setTip(intrebareTO.getTip());
		return intrebareE.getIntrebareTO();
	}

	@Override
	public void delIntrebare(long id) {
		IntrebareE intrebareE = this.em.find(IntrebareE.class, id);
		this.em.remove(intrebareE);

	}

	@Override
	public IntrebareTO getIntrebare(long id) {
		Query query = em.createNamedQuery("IntrebareE.findByID");
		query.setParameter("id", id);

		if (!query.getResultList().isEmpty()) {
			IntrebareE intrebareE = (IntrebareE) query.getResultList().get(0);
			return intrebareE.getIntrebareTO();
		}
		return null;
	}

	@Override
	public List<IntrebareTO> getIntrebari() {
		// TODO Auto-generated method stub
		return null;
	}

}